import copy
import inspect
import os
import shutil
import subprocess
import sys
import datetime
import argparse
import re
import execute_common
from ssh_client import SSHClient
from hdc_client import HDCClient
from execute_report import *
from execute_command import *
from execute_log import *

CASE_FILE_SUFFIX = ['ts', 'js']
SUMMARY_REPORT = ''
CLIENT_LOGGER = server_logger
execute_command = ExecuteCommand()

def collect_names_of_case():
    case_infos = []
    for (cur_path, directorys, files) in os.walk(execute_common.TEST_SUITE_SRC_PATH):
        if cur_path != execute_common.TEST_SUITE_SRC_PATH:
            continue
        for file in files:
            for suffix in CASE_FILE_SUFFIX:
                if file.endswith(f'.{suffix}'):
                    report_detail = ReportDetail(file[:-len(suffix) - 1])
                    case_infos.append((report_detail, suffix))
                    break
        for directory in directorys:
            report_detail = ReportDetail(directory)
            case_infos.append((report_detail, 'dir'))
    return case_infos

def filter_test_cases(case_infos, args):
    if args.one_case == '':
        return case_infos
    filter_result = []
    for report_detail, suf in case_infos:
        if report_detail.get_case_name() == args.one_case:
            filter_result.append((report_detail, suf))
            break
    return filter_result

def execute_gen_abc(case_infos):
    global SUMMARY_REPORT
    cur_time = datetime.datetime.now()
    os.chdir(execute_common.TEST_SUITE_SRC_PATH)
    cmd = ''
    report_details = []
    for report_detail, suf in case_infos:
        cmd = [
            f'{execute_common.X64_TOOL_CHAIN_FULL_PATH}/arkcompiler/ets_frontend/es2abc ',
            f'--module ',
            f'--merge-abc ',
            f'--output ',
            f'{execute_common.TEST_SUITE_ABC_PATH}/{report_detail.get_case_name()}.abc ',
        ]
        if suf == 'dir':
            cmd.append(f'@{report_detail.get_case_name()}/fileInfo.txt')
        else:
            cmd.append(f'{report_detail.get_case_name()}.{suf}')
        execute_succeeded, log = execute_command.execute(cmd, server_logger)
        report_detail.set_execute_successful(execute_succeeded)
        report_detail.set_error_message(log)
        report_details.append(report_detail)
    time_used_by_es2abc = datetime.datetime.now() - cur_time
    SUMMARY_REPORT = SUMMARY_REPORT + 'time used by es2abc: ' + str(time_used_by_es2abc) + 's\n'
    return report_details

def collect_ark_js_vm_result(execute_succeeded, out, report_detail, split_char = '\r\n'):
    CLIENT_LOGGER.info(out)
    exit_status = ''
    for test_point in out.split(split_char):
        if test_point != '\r\n' and test_point != '\n' and test_point != '':
            # Get the last line in the result, which is "echo $?" result.
            exit_status = test_point
        case_name, time = get_case_name_and_time_from_str(test_point)
        # Test points with the same name will be overwritten and only the last one will be recorded.
        if case_name == None:
            continue
        report_detail.add_test_point(case_name, time)
    if execute_succeeded and exit_status != '0':
        execute_succeeded = False
    report_detail.set_execute_successful(execute_succeeded)
    if not execute_succeeded:
        CLIENT_LOGGER.error(f'execute {report_detail.get_case_name()} fail.')
        report_detail.set_error_message(out)

def collect_ark_aot_compiler_result(execute_succeeded, out, report_detail, split_char = '\r\n'):
    CLIENT_LOGGER.info(out)
    exit_status = ''
    for test_point in out.split(split_char):
        if test_point != '\r\n' and test_point != '\n' and test_point != '':
            # Get the last line in the result, which is "echo $?" result.
            exit_status = test_point
    if execute_succeeded and exit_status != '0':
        execute_succeeded = False
    report_detail.set_execute_successful(execute_succeeded)
    if not execute_succeeded:
        CLIENT_LOGGER.error(f'execute {report_detail.get_case_name()} fail.')
        report_detail.set_error_message(out)

def set_x64_environ():
    ld_library_path = [
        f'{execute_common.X64_TOOL_CHAIN_FULL_PATH}/arkcompiler/ets_runtime:',
        f'{execute_common.X64_TOOL_CHAIN_FULL_PATH}/thirdparty/bounds_checking_function:',
        f'{execute_common.X64_TOOL_CHAIN_FULL_PATH}/thirdparty/icu:',
        f'{execute_common.X64_TOOL_CHAIN_FULL_PATH}/thirdparty/zlib:',
    ]
    ld_library_path = ''.join(ld_library_path)
    CLIENT_LOGGER.info(f'LD_LIBRARY_PATH: {ld_library_path}')
    os.environ['LD_LIBRARY_PATH'] = ld_library_path

def execute_x64_interpreter(report_details, need_pgo, need_an):
    global SUMMARY_REPORT
    cur_time = datetime.datetime.now()
    os.chdir(execute_common.TEST_SUITE_ABC_PATH)
    for report_detail in report_details:
        interpreter_cmd = [
            f'{execute_common.X64_TOOL_CHAIN_FULL_PATH}/arkcompiler/ets_runtime/ark_js_vm ',
            f'--icu-data-path={execute_common.LOCAL_CODE_REPOSITORY_PATH}/third_party/icu/ohos_icu4j/data ',
            f'--stub-file={execute_common.X64_TOOL_CHAIN_FULL_PATH}/gen/arkcompiler/ets_runtime/stub.an ',
        ]
        if need_pgo:
            interpreter_cmd = interpreter_cmd + [f'--enable-pgo-profiler=true ', f'--compiler-pgo-profiler-path={report_detail.get_case_name()}.ap ']
        if need_an:
            interpreter_cmd = interpreter_cmd + [f'--aot-file={report_detail.get_case_name()} ']
        interpreter_cmd = interpreter_cmd + [f'--entry-point={report_detail.get_case_name()} ',  f'{execute_common.TEST_SUITE_ABC_PATH}/{report_detail.get_case_name()}.abc', f' ; echo $?']
        execute_succeeded, out = execute_command.execute(interpreter_cmd, server_logger, 3600)
        collect_ark_js_vm_result(execute_succeeded, out, report_detail, '\n')
    time_used_by_execute_cmd_in_x64 = datetime.datetime.now() - cur_time
    SUMMARY_REPORT = SUMMARY_REPORT + "time used by execute cmd in x64: " + str(time_used_by_execute_cmd_in_x64) + 's\n'
    return report_details

def execute_x64_jit(report_details):
    global SUMMARY_REPORT
    cur_time = datetime.datetime.now()
    os.chdir(execute_common.TEST_SUITE_ABC_PATH)
    ark_js_vm_path = os.path.join(execute_common.X64_TOOL_CHAIN_FULL_PATH, 'arkcompiler/ets_runtime/ark_js_vm')
    stub_path = os.path.join(execute_common.X64_TOOL_CHAIN_FULL_PATH, 'gen/arkcompiler/ets_runtime/stub.an')
    icu_path = os.path.join(execute_common.LOCAL_CODE_REPOSITORY_PATH, 'third_party/icu/ohos_icu4j/data')
    for report_detail in report_details:
        jit_cmd = [
            f'{ark_js_vm_path} ',
            f'--compiler-enable-jit=true ',
            f'--compiler-enable-litecg=true ',
            f'--compiler-jit-hotness-threshold=2 ',
            f'--compiler-opt-inlining=false ',
            f'--compiler-opt-track-field=false ',
            f'--entry-point={report_detail.get_case_name()} ',
            f'--icu-data-path={icu_path} ',
            f'--stub-file={stub_path} ',
            f'{execute_common.TEST_SUITE_ABC_PATH}/{report_detail.get_case_name()}.abc',
            f'; echo $?',
        ]
        execute_succeeded, out = execute_command.execute(jit_cmd, server_logger, 3600)
        collect_ark_js_vm_result(execute_succeeded, out, report_detail, '\n')
    time_used_by_execute_cmd_in_x64 = datetime.datetime.now() - cur_time
    SUMMARY_REPORT = SUMMARY_REPORT + "time used by execute cmd in x64: " + str(time_used_by_execute_cmd_in_x64) + 's\n'
    return

def execute_x64_aot_compiler(report_details):
    global SUMMARY_REPORT
    cur_time = datetime.datetime.now()
    os.chdir(execute_common.TEST_SUITE_ABC_PATH)
    for report_detail in report_details:
        compile_cmd = [
            f'{execute_common.X64_TOOL_CHAIN_FULL_PATH}/arkcompiler/ets_runtime/ark_aot_compiler ',
            f'--aot-file={report_detail.get_case_name()} ',
            f'--compiler-enable-litecg=true ',
            f'--compiler-opt-track-field=false ',
            f'--compiler-pgo-profiler-path={report_detail.get_case_name()}.ap ',
            f'{execute_common.TEST_SUITE_ABC_PATH}/{report_detail.get_case_name()}.abc',
            f' ; echo $?',
        ]
        execute_succeeded, out = execute_command.execute(compile_cmd, server_logger, 3600)
        collect_ark_aot_compiler_result(execute_succeeded, out, report_detail, '\n')
    time_used_by_execute_x64_aot_compiler = datetime.datetime.now() - cur_time
    SUMMARY_REPORT = SUMMARY_REPORT + "time used by execute_x64_aot_compiler: " + str(time_used_by_execute_x64_aot_compiler) + 's\n'
    return report_details

def set_qemu64_environ(code_repository_type):
    ld_library_path = [
        f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/arkcompiler/ets_runtime:',
        f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/thirdparty/zlib:',
        f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/thirdparty/icu:',
        f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/thirdparty/bounds_checking_function:',
        f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/common/common/libc:',
    ]
    if code_repository_type == 'openharmony':
        ld_library_path = ld_library_path + [
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/commonlibrary/c_utils:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/hiviewdfx/faultloggerd:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/hiviewdfx/hilog:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/hiviewdfx/hisysevent:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/hiviewdfx/hitrace:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/resourceschedule/ffrt:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/resourceschedule/qos_manager:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/startup/init:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/thirdparty/libuv:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/thirdparty/lzma:',
            f'{execute_common.LOCAL_CODE_REPOSITORY_PATH}/prebuilts/clang/ohos/linux-x86_64/llvm/lib/aarch64-linux-ohos:',
            f'{execute_common.LOCAL_CODE_REPOSITORY_PATH}/prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/15.0.4/lib/aarch64-linux-ohos:',
        ]
    if code_repository_type == 'openharmony_dev' or code_repository_type == 'openharmony_trunk':
        ld_library_path = ld_library_path + [
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/commonlibrary/c_utils:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/hiviewdfx/faultloggerd:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/hiviewdfx/hilog_override:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/hiviewdfx/hisysevent:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/hiviewdfx/hitrace:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/obj/third_party/musl/usr/lib/aarch64-linux-ohos:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/resourceschedule/ffrt:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/resourceschedule/qos_manager:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/startup/init:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/thirdparty/cJSON:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/thirdparty/libuv:',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/thirdparty/lzma:',
            f'{execute_common.LOCAL_CODE_REPOSITORY_PATH}/prebuilts/clang/ohos/linux-x86_64/llvm/lib/aarch64-linux-ohos:',
            f'{execute_common.LOCAL_CODE_REPOSITORY_PATH}/prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/15.0.4/lib/aarch64-linux-ohos:',
        ]
    ld_library_path = ''.join(ld_library_path)
    CLIENT_LOGGER.info(f'LD_LIBRARY_PATH: {ld_library_path}')
    os.environ['LD_LIBRARY_PATH'] = ld_library_path

def execute_qemu64_interpreter(report_details, need_pgo, need_an):
    global SUMMARY_REPORT
    cur_time = datetime.datetime.now()
    os.chdir(execute_common.TEST_SUITE_ABC_PATH)
    for report_detail in report_details:
        interpreter_cmd = [
            f'qemu-aarch64 -L {execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/common/common/libc ',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/arkcompiler/ets_runtime/ark_js_vm ',
            f'--icu-data-path={execute_common.LOCAL_CODE_REPOSITORY_PATH}/third_party/icu/ohos_icu4j/data ',
            f'--stub-file={execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/gen/arkcompiler/ets_runtime/stub.an ',
        ]
        if need_pgo:
            interpreter_cmd = interpreter_cmd + [f'--enable-pgo-profiler=true ', f'--compiler-pgo-profiler-path={report_detail.get_case_name()}.ap ']
        if need_an:
            interpreter_cmd = interpreter_cmd + [f'--aot-file={report_detail.get_case_name()} ']
        interpreter_cmd = interpreter_cmd + [f'--entry-point={report_detail.get_case_name()} ',  f'{execute_common.TEST_SUITE_ABC_PATH}/{report_detail.get_case_name()}.abc', f' ; echo $?']
        execute_succeeded, out = execute_command.execute(interpreter_cmd, server_logger, 3600)
        collect_ark_js_vm_result(execute_succeeded, out, report_detail, '\n')
    time_used_by_execute_cmd_in_x64 = datetime.datetime.now() - cur_time
    SUMMARY_REPORT = SUMMARY_REPORT + "time used by execute cmd in x64: " + str(time_used_by_execute_cmd_in_x64) + 's\n'
    return report_details

def execute_qemu64_jit(report_details):
    global SUMMARY_REPORT
    cur_time = datetime.datetime.now()
    os.chdir(execute_common.TEST_SUITE_ABC_PATH)
    for report_detail in report_details:
        jit_cmd = [
            f'qemu-aarch64 -L {execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/common/common/libc ',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/arkcompiler/ets_runtime/ark_js_vm ',
            f'--compiler-enable-jit=true ',
            f'--compiler-enable-litecg=true ',
            f'--compiler-jit-hotness-threshold=2 ',
            f'--compiler-opt-inlining=false ',
            f'--compiler-opt-track-field=false ',
            f'--entry-point={report_detail.get_case_name()} ',
            f'--icu-data-path={execute_common.LOCAL_CODE_REPOSITORY_PATH}/third_party/icu/ohos_icu4j/data ',
            f'--stub-file={execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/gen/arkcompiler/ets_runtime/stub.an ',
            f'{execute_common.TEST_SUITE_ABC_PATH}/{report_detail.get_case_name()}.abc',
            f'; echo $?',
        ]
        execute_succeeded, out = execute_command.execute(jit_cmd, server_logger, 3600)
        collect_ark_js_vm_result(execute_succeeded, out, report_detail, '\n')
    time_used_by_execute_cmd_in_x64 = datetime.datetime.now() - cur_time
    SUMMARY_REPORT = SUMMARY_REPORT + "time used by execute cmd in x64: " + str(time_used_by_execute_cmd_in_x64) + 's\n'
    return

def execute_qemu64_aot_compiler(report_details):
    global SUMMARY_REPORT
    cur_time = datetime.datetime.now()
    os.chdir(execute_common.TEST_SUITE_ABC_PATH)
    for report_detail in report_details:
        compile_cmd = [
            f'qemu-aarch64 -L {execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/common/common/libc ',
            f'{execute_common.AARCH64_TOOL_CHAIN_FULL_PATH}/arkcompiler/ets_runtime/ark_aot_compiler ',
            f'--compiler-enable-litecg=true ',
            f'--compiler-opt-track-field=false ',
            f'--compiler-pgo-profiler-path={report_detail.get_case_name()}.ap ',
            f'--compiler-target-triple=aarch64-unknown-linux-gnu ',
            f'--aot-file={report_detail.get_case_name()} ',
            f'{execute_common.TEST_SUITE_ABC_PATH}/{report_detail.get_case_name()}.abc',
            f' ; echo $?',
        ]
        execute_succeeded, out = execute_command.execute(compile_cmd, server_logger, 3600)
        collect_ark_aot_compiler_result(execute_succeeded, out, report_detail, '\n')
    time_used_by_execute_x64_aot_compiler = datetime.datetime.now() - cur_time
    SUMMARY_REPORT = SUMMARY_REPORT + "time used by execute_x64_aot_compiler: " + str(time_used_by_execute_x64_aot_compiler) + 's\n'
    return report_details

def copy_ets_runtime_aarch64_lib(des_dir, code_repository_type):
    lib_paths = []
    lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'arkcompiler/ets_runtime/ark_aot_compiler'))
    lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'arkcompiler/ets_runtime/ark_js_vm'))
    lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'arkcompiler/ets_runtime/libark_jsoptimizer.so'))
    lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'arkcompiler/ets_runtime/libark_jsruntime.so'))
    lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'arkcompiler/ets_runtime/libark_llvmcodegen.so'))
    lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'gen/arkcompiler/ets_runtime/stub.an'))
    if code_repository_type == 'standalone':
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'thirdparty/zlib/libshared_libz.so'))
    elif code_repository_type == 'openharmony' or code_repository_type == 'openharmony_dev' or code_repository_type == 'openharmony_trunk':
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'commonlibrary/c_utils/libutils.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'communication/ipc/libipc_common.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'communication/ipc/libipc_single.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'hiviewdfx/faultloggerd/libbacktrace_local.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'hiviewdfx/faultloggerd/libdfx_procinfo.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'hiviewdfx/faultloggerd/libfaultloggerd.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'hiviewdfx/faultloggerd/libunwinder.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'hiviewdfx/hisysevent/libhisysevent.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'hiviewdfx/hitrace/libhitrace_meter.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'hiviewdfx/hitrace/libhitracechain.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'hiviewdfx/hiview/libucollection_client.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'resourceschedule/ffrt/libffrt.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'resourceschedule/qos_manager/libqos.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'startup/init/libbegetutil.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'thirdparty/bounds_checking_function/libsec_shared.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'thirdparty/cJSON/libcjson.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'thirdparty/icu/libhmicui18n.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'thirdparty/icu/libhmicuuc.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'thirdparty/libuv/libuv.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'thirdparty/lzma/liblzma.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'thirdparty/zlib/libshared_libz.z.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'systemabilitymgr/samgr/libsamgr_proxy.z.so'))
    else:
        server_logger.error(f'Unsupported code_repository_type: {code_repository_type}')
        return False
    if code_repository_type == 'openharmony':
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'hiviewdfx/hilog/libhilog.so'))
    if code_repository_type == 'openharmony_dev' or code_repository_type == 'openharmony_trunk':
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'hiviewdfx/hilog_override/libhilog.so'))
        lib_paths.append(os.path.join(execute_common.AARCH64_TOOL_CHAIN_FULL_PATH, 'security/code_signature/libjit_code_sign.z.so'))
    execute_succeeded, log = execute_command.execute(['rm -rf ', des_dir, '/*'], server_logger)
    if not execute_succeeded:
        return False
    for lib_path in lib_paths:
        execute_succeeded, log = execute_command.execute(['cp ', lib_path, ' ', des_dir], server_logger)
        if not execute_succeeded:
            return False
    return True

def send_abc_and_lib_to_mobile(ssh_client, hdc_client, abc_path, lib_path):
    global SUMMARY_REPORT
    cur_time = datetime.datetime.now()
    # send lib to mobile
    if not ssh_client.mkdir_if_not_exists(execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_PC):
        return False
    if not ssh_client.clear_directory(execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_PC):
        return False
    execute_succeeded, out = hdc_client.execute(f'rm {execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE}/*')
    if not execute_succeeded:
        return False
    execute_succeeded, out = hdc_client.execute(f'mkdir -p {execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE}')
    if not execute_succeeded:
        return False
    for (cur_path, directorys, files) in os.walk(lib_path):
        for file in files:
            lib_file_path_in_server = os.path.join(cur_path, file)
            lib_file_path_in_pc = f'{execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_PC}\\{file}'
            lib_file_path_in_mobile = f'{execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE}/{file}'
            if not ssh_client.send_file(lib_file_path_in_server, lib_file_path_in_pc):
                return False
            if not hdc_client.send_file(lib_file_path_in_pc, lib_file_path_in_mobile):
                return False
            execute_succeeded, out = hdc_client.execute(f'chmod 755 {lib_file_path_in_mobile}')
            if not execute_succeeded:
                return False
    # send abc to mobile
    if not ssh_client.mkdir_if_not_exists(execute_common.TEST_SUITE_ABC_PATH_IN_PC):
        return False
    if not ssh_client.clear_directory(execute_common.TEST_SUITE_ABC_PATH_IN_PC):
        return False
    execute_succeeded, out = hdc_client.execute(f'rm {execute_common.TEST_SUITE_ABC_PATH_IN_MOBILE}/*')
    if not execute_succeeded:
        return False
    execute_succeeded, out = hdc_client.execute(f'mkdir -p {execute_common.TEST_SUITE_ABC_PATH_IN_MOBILE}')
    if not execute_succeeded:
        return False
    for (cur_path, directorys, files) in os.walk(abc_path):
        for file in files:
            abc_file_path_in_server = os.path.join(cur_path, file)
            abc_file_path_in_pc = f'{execute_common.TEST_SUITE_ABC_PATH_IN_PC}\\{file}'
            abc_file_path_in_mobile = f'{execute_common.TEST_SUITE_ABC_PATH_IN_MOBILE}/{file}'
            if not ssh_client.send_file(abc_file_path_in_server, abc_file_path_in_pc):
                return False
            if not hdc_client.send_file(abc_file_path_in_pc, abc_file_path_in_mobile):
                return False
    time_used_by_send_abc_and_lib_to_mobile = datetime.datetime.now() - cur_time
    SUMMARY_REPORT = SUMMARY_REPORT + 'time used by send abc and lib to mobile: ' + str(time_used_by_send_abc_and_lib_to_mobile) + 's\n'
    return True

def send_core_bat_to_pc(ssh_client):
    if not ssh_client.mkdir_if_not_exists(execute_common.TEST_SUITE_SCRIPT_PATH_IN_PC):
        return False
    if not ssh_client.clear_directory(execute_common.TEST_SUITE_SCRIPT_PATH_IN_PC):
        return False
    if not ssh_client.send_file(execute_common.HOS_SMALL_CORE_MAX_FREQ_BAT, execute_common.HOS_SMALL_CORE_MAX_FREQ_BAT_IN_PC):
        return False
    if not ssh_client.send_file(execute_common.HOS_ALL_CORE_MAX_FREQ_60, execute_common.HOS_ALL_CORE_MAX_FREQ_60_IN_PC):
        return False
    return True

def execute_aarch64_interpreter(hdc_client, report_details, need_pgo, need_an):
    global SUMMARY_REPORT
    cur_time = datetime.datetime.now()
    os.chdir(execute_common.TEST_SUITE_ABC_PATH)
    for report_detail in report_details:
        interpreter_cmd = [
            f'\"',
            f'cd {execute_common.TEST_SUITE_ABC_PATH_IN_MOBILE} ',
            f'&& export LD_LIBRARY_PATH={execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE} ',
            f'&& {execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE}/ark_js_vm ',
            f'--icu-data-path={execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE} ',
            f'--open-ark-tools=true ',
            f'--stub-file={execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE}/stub.an ',
        ]
        if need_pgo:
            interpreter_cmd = interpreter_cmd + [f'--enable-pgo-profiler=true ', f'--compiler-pgo-profiler-path={report_detail.get_case_name()}.ap ']
        if need_an:
            interpreter_cmd = interpreter_cmd + [f'--aot-file={report_detail.get_case_name()} ']
        interpreter_cmd = interpreter_cmd + [f'--entry-point={report_detail.get_case_name()} ',  f'{execute_common.TEST_SUITE_ABC_PATH_IN_MOBILE}/{report_detail.get_case_name()}.abc', f' ; echo $?', f'\"']
        execute_succeeded, out = hdc_client.execute(''.join(interpreter_cmd), 3600)
        collect_ark_js_vm_result(execute_succeeded, out, report_detail)
    time_used_by_execute_cmd_in_mobile = datetime.datetime.now() - cur_time
    SUMMARY_REPORT = SUMMARY_REPORT + "time used by execute cmd in mobile: " + str(time_used_by_execute_cmd_in_mobile) + 's\n'
    return report_details

def execute_aarch64_jit(hdc_client, report_details):
    global SUMMARY_REPORT
    cur_time = datetime.datetime.now()
    os.chdir(execute_common.TEST_SUITE_ABC_PATH)
    for report_detail in report_details:
        jit_cmd = [
            f'\"',
            f'cd {execute_common.TEST_SUITE_ABC_PATH_IN_MOBILE} ',
            f'&& export LD_LIBRARY_PATH={execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE} ',
            f'&& {execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE}/ark_js_vm ',
            f'--compiler-enable-jit=true ',
            f'--compiler-enable-litecg=true ',
            f'--compiler-jit-hotness-threshold=2 '
            f'--compiler-opt-inlining=false ',
            f'--compiler-opt-track-field=false ',
            f'--entry-point={report_detail.get_case_name()} ',
            f'--icu-data-path={execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE} ',
            f'--open-ark-tools=true ',
            f'--stub-file={execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE}/stub.an ',
            f'{execute_common.TEST_SUITE_ABC_PATH_IN_MOBILE}/{report_detail.get_case_name()}.abc',
            f' ; echo $?',
            f'\"',
        ]
        execute_succeeded, out = hdc_client.execute(''.join(jit_cmd), 3600)
        collect_ark_js_vm_result(execute_succeeded, out, report_detail)
    time_used_by_execute_cmd_in_mobile = datetime.datetime.now() - cur_time
    SUMMARY_REPORT = SUMMARY_REPORT + "time used by execute cmd in mobile: " + str(time_used_by_execute_cmd_in_mobile) + 's\n'
    return report_details

def execute_aarch64_aot_compiler(hdc_client, report_details):
    global SUMMARY_REPORT
    cur_time = datetime.datetime.now()
    os.chdir(execute_common.TEST_SUITE_ABC_PATH)
    for report_detail in report_details:
        compile_cmd = [
            f'\"',
            f'cd {execute_common.TEST_SUITE_ABC_PATH_IN_MOBILE} '
            f'&& export LD_LIBRARY_PATH={execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE} ',
            f'&& {execute_common.ETS_RUNTIME_AARCH64_LIB_PATH_IN_MOBILE}/ark_aot_compiler ',
            f'--aot-file={report_detail.get_case_name()} ',
            f'--compiler-enable-litecg=true ',
            f'--compiler-opt-track-field=false ',
            f'--compiler-pgo-profiler-path={report_detail.get_case_name()}.ap ',
            f'--compiler-target-triple=aarch64-unknown-linux-gnu ',
            f'{execute_common.TEST_SUITE_ABC_PATH_IN_MOBILE}/{report_detail.get_case_name()}.abc',
            f' ; echo $?',
            f'\"',
        ]
        execute_succeeded, out = hdc_client.execute(''.join(compile_cmd), 3600)
        collect_ark_aot_compiler_result(execute_succeeded, out, report_detail)
    time_used_by_execute_cmd_in_mobile = datetime.datetime.now() - cur_time
    SUMMARY_REPORT = SUMMARY_REPORT + "time used by execute cmd in mobile: " + str(time_used_by_execute_cmd_in_mobile) + 's\n'
    return report_details

def main(args):
    print("execute_workload_main")
    global SUMMARY_REPORT
    global CLIENT_LOGGER
    begin_time = datetime.datetime.now()
    client_log_path = os.path.join(execute_common.TEST_SUITE_OUT_PATH, f'{args.test_suite_name}_{args.mode}_{args.sn}_{begin_time.now().strftime("%Y%m%d_%H%M%S")}.log')
    CLIENT_LOGGER = ExecuteLog('client_logger', client_log_path)
    case_infos = collect_names_of_case()
    case_infos = filter_test_cases(case_infos, args)
    if not case_infos:
        print("The test case cannot be found in the test suite path: " + execute_common.TEST_SUITE_SRC_PATH)
        return False
    all_report_details = []
    if args.mode == "gen_abc":
        report_details = execute_gen_abc(case_infos)
    elif args.mode == 'x64_interpreter' or args.mode == 'x64_jit' or args.mode == 'x64_aot':
        # gen abc
        report_details = execute_gen_abc(case_infos)
        # set LD_LIBRARY_PATH
        set_x64_environ();
        # execute interpreter command in x64
        if args.mode == 'x64_interpreter':
            execute_x64_interpreter(report_details, True, False)
        # execute jit command in x64
        elif args.mode == 'x64_jit':
            execute_x64_jit(report_details)
        # execute aot command in x64
        elif args.mode == 'x64_aot':
            # execute interpreter with pgo
            execute_x64_interpreter(report_details, True, False)
            execute_x64_aot_compiler(report_details)
            # execute interpreter with pgo and an
            execute_x64_interpreter(report_details, True, True)
            execute_x64_aot_compiler(report_details)
            # execute interpreter with an
            execute_x64_interpreter(report_details, False, True)
        all_report_details.append(report_details)
    elif args.mode == 'qemu64_interpreter' or args.mode == 'qemu64_jit' or args.mode == 'qemu64_aot':
        # gen abc
        report_details = execute_gen_abc(case_infos)
        # set LD_LIBRARY_PATH
        set_qemu64_environ(args.code_repository_type)
        # execute interpreter command in qemu64
        if args.mode == 'qemu64_interpreter':
            execute_qemu64_interpreter(report_details, True, False)
        # execute jit command in qemu64
        elif args.mode == 'qemu64_jit':
            execute_qemu64_jit(report_details)
        # execute aot command in qemu64
        elif args.mode == 'qemu64_aot':
            # execute interpreter with pgo
            execute_qemu64_interpreter(report_details, True, False)
            execute_qemu64_aot_compiler(report_details)
            # execute interpreter with pgo and an
            execute_qemu64_interpreter(report_details, True, True)
            execute_qemu64_aot_compiler(report_details)
            # execute interpreter with an
            execute_qemu64_interpreter(report_details, False, True)
        all_report_details.append(report_details)
    elif args.mode == 'aarch64_interpreter' or args.mode == 'aarch64_jit' or args.mode == 'aarch64_aot':
        # gen abc
        report_details = execute_gen_abc(case_infos)
        # back up lib file.
        if not copy_ets_runtime_aarch64_lib(execute_common.ETS_RUNTIME_AARCH64_LIB_AFTER, args.code_repository_type):
            print("copy ets_runtime aarch64 lib fail!")
            return False
        # send abc and lib to mobile
        ssh_client = SSHClient(args.ssh_user, args.ssh_password, args.ssh_ip, args.ssh_port, get_pty=False)
        hdc_client = HDCClient(args.sn, ssh_client)
        execute_succeeded = hdc_client.mount()
        if not execute_succeeded:
            return False
        if not send_abc_and_lib_to_mobile(ssh_client, hdc_client, execute_common.TEST_SUITE_ABC_PATH, execute_common.ETS_RUNTIME_AARCH64_LIB_AFTER):
            return False
        # send HOS core bat
        if not send_core_bat_to_pc(ssh_client):
            return False
        # execute interpreter command in mobile
        if args.mode == 'aarch64_interpreter':
            ssh_client.execute(f'{execute_common.HOS_SMALL_CORE_MAX_FREQ_BAT_IN_PC} {args.sn}')
            for count in range(int(args.repetition_count)):
                tmp_report_details = copy.deepcopy(report_details)
                # execute interpreter with pgo
                tmp_report_details = execute_aarch64_interpreter(hdc_client, tmp_report_details, True, False)
                all_report_details.append(tmp_report_details)
            ssh_client.execute(f'{execute_common.HOS_ALL_CORE_MAX_FREQ_60_IN_PC} {args.sn}')
        # execute jit command in mobile
        elif args.mode == 'aarch64_jit':
            ssh_client.execute(f'{execute_common.HOS_SMALL_CORE_MAX_FREQ_BAT_IN_PC} {args.sn}')
            for count in range(int(args.repetition_count)):
                tmp_report_details = copy.deepcopy(report_details)
                tmp_report_details = execute_aarch64_jit(hdc_client, tmp_report_details)
                all_report_details.append(tmp_report_details)
            ssh_client.execute(f'{execute_common.HOS_ALL_CORE_MAX_FREQ_60_IN_PC} {args.sn}')
        # execute aot command in mobile
        elif args.mode == 'aarch64_aot':
            # execute interpreter with pgo
            report_details = execute_aarch64_interpreter(hdc_client, report_details, True, False)
            report_details = execute_aarch64_aot_compiler(hdc_client, report_details)
            # execute interpreter with pgo and an
            report_details = execute_aarch64_interpreter(hdc_client, report_details, True, True)
            report_details = execute_aarch64_aot_compiler(hdc_client, report_details)
            ssh_client.execute(f'{execute_common.HOS_SMALL_CORE_MAX_FREQ_BAT_IN_PC} {args.sn}')
            for count in range(int(args.repetition_count)):
                tmp_report_details = copy.deepcopy(report_details)
                tmp_report_details = execute_aarch64_interpreter(hdc_client, tmp_report_details, False, True)
                all_report_details.append(tmp_report_details)
            ssh_client.execute(f'{execute_common.HOS_ALL_CORE_MAX_FREQ_60_IN_PC} {args.sn}')
        del ssh_client
    else:
        report_details = []
        for report_detail, suf in case_infos:
            report_details.append(report_detail)
        all_report_details.append(report_details)
        print("Unsupported value for mode: " + args.mode)
    execute_report = ExecuteReport(args.test_suite_name, args.mode, args.sn, begin_time, all_report_details)
    execute_report.report(CLIENT_LOGGER)
    execute_report.report_summary(SUMMARY_REPORT, CLIENT_LOGGER)
    return True
